package com.txc.books.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txc.books.entity.Book;
import com.txc.books.entity.BookArea;
import com.txc.books.entity.User;
import com.txc.books.mapper.BookAreaMapper;
import com.txc.books.service.IBookAreaService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import com.txc.books.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: XCTANG
 *
 */
@Service
public class BookAreaServiceImpl implements IBookAreaService {

    private final BookAreaMapper bookAreaMapper;

    public BookAreaServiceImpl(BookAreaMapper bookAreaMapper){
        this.bookAreaMapper = bookAreaMapper;
    }

    @Override
    public List<BookArea> findAll() {
        return bookAreaMapper.findAll();
    }

    @Override
    public void insert(BookArea bookArea) {
        String name = bookArea.getName();

        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        bookAreaMapper.insert(bookArea);
    }

    @Override
    public void update(BookArea bookArea) {

        String name = bookArea.getName();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        bookAreaMapper.update(bookArea);
    }

    @Override
    public void delete(Integer id) {
        bookAreaMapper.delete(id);
    }

    @Override
    public void select(Integer id) {
        bookAreaMapper.select(id);
    }

    @Override
    public PageResult findByPager(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    public PageInfo<BookArea> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BookArea> areaMenus = bookAreaMapper.selectPage();
        return new PageInfo<BookArea>(areaMenus);
    }
}
